/**
 * 
 */
package com.example.rabbitmqTest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * rabbitmq配置
 * @author yanglei
 * 2018年1月15日
 */
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;  
  

@Configuration  
public class RabbitMqConfig {  
  
    @Autowired  
    private Environment env;  
  
    @Autowired  
    private CachingConnectionFactory connectionFactory;  
    
    private final static Logger log = LoggerFactory.getLogger("mqLog");
    /*@Autowired  
    private ConnectionFactory connectionFactory;*/  
  
    @Bean  
    public RabbitTemplate rabbitTemplate(){  
    	connectionFactory.setPublisherConfirms(true);
		connectionFactory.setPublisherReturns(true);
		RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMandatory(true);
		rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
			@Override
			public void confirm(CorrelationData correlationData, boolean ack, String cause) {
				log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);
			}
		});
		rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
			@Override
			public void returnedMessage(Message message, int replyCode, String replyText, String exchange,
					String routingKey) {
				log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey,
						replyCode, replyText, message);
			}
		});
        return rabbitTemplate;  
    }  
  
    //TODO: DirectExchange+routingKey -> Queue 队列绑定  
  
    @Bean  
    public DirectExchange receiptExchange(){  
        return new DirectExchange(env.getProperty("mq.second.queue.exchange"),true,false);  
    }  
  
    @Bean  
    public Queue receiptQueue(){  
        return new Queue(env.getProperty("mq.second.queue.name"),true);  
    }  
  
    @Bean  
    public Binding receiptBinding(){  
        return BindingBuilder.bind(receiptQueue()).to(receiptExchange()).with(env.getProperty("mq.second.queue.routingKey"));  
    }  
    
    
    
    @Bean  
    public DirectExchange receiptExchange2(){  
        return new DirectExchange("exchange2",true,false);  
    }  
  
    @Bean  
    public Queue receiptQueue2(){  
        return new Queue("queue2",true);  
    }  
  
    @Bean  
    public Binding receiptBinding2(){  
        return BindingBuilder.bind(receiptQueue()).to(receiptExchange2()).with("routingkey2");  
    }  
  
    
}  
